package com.oceanstar.controller;

import com.oceanstar.Pojo.*;
import com.oceanstar.service.IChaperonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class ChaperonController {

    @Autowired
    private IChaperonService chaperonService;

    /*
     *新增陪诊人员
     */
    @PostMapping("/services/chaperon")
    public Result addChaperon(@RequestBody ChaperonInfo chaperonInfo, @RequestHeader("token") String token){
        try {
            chaperonService.addChaperon(chaperonInfo,token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("添加失败" + e.getMessage());
        }
    }

    /*
     *删除陪诊人员
     */
    @DeleteMapping("/services/chaperon")
    public Result deleteChaperon(@RequestParam("id") Integer id, @RequestHeader("token") String token){
        try {
            chaperonService.deleteChaperon(id,token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除失败" + e.getMessage());
        }
    }

    /*
     *修改陪诊人员信息
     */
    @PatchMapping("/services/chaperon")
    public Result updateChaperonInfo(@RequestBody ChaperonInfo chaperonInfo) {
        try {
            chaperonService.updateChaperonInfo(chaperonInfo);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    /*
     *查询所有陪诊人员的数据信息
     */
    @GetMapping("/services/chaperon/all")
    public Result getAllChaperonInfo(){
        try {
            List<ChaperonInfo> chaperonInfoList = chaperonService.getAllChaperonInfo();
            return Result.success(chaperonInfoList);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *查询陪诊人员的数据信息
     */
    @GetMapping("/services/chaperon")
    public Result getChaperonInfo(@RequestParam("id") Integer id){
        try {
            ChaperonInfo chaperonInfo = chaperonService.getChaperonInfo(id);
            return Result.success(chaperonInfo);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *管理员发布新的陪诊服务
     */
    @PostMapping("/services")
    public Result createNewService(@RequestBody ServiceInfo serviceInfo, @RequestHeader("token") String token){
        try {
            chaperonService.updateServicesInfo(serviceInfo,token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("发布失败: " + e.getMessage());
        }
    }

    /*
     *管理员获取陪诊列表
     */
    @GetMapping("/services/all")
    public Result getAllServices(@RequestHeader("token") String token){
        try {
            PageInfo<Services> pageInfo = chaperonService.getServicesList(token);
            return Result.success(pageInfo);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *管理员下架陪诊服务
     */
    @DeleteMapping("/services")
    public Result deleteService(@RequestParam("id") Integer id, @RequestHeader("token") String token){
        try {
            chaperonService.deleteService(id,token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    /*
     *获取陪诊列表,方便老人进行选择预约
     */
    @GetMapping("/services")
    public Result getServices(@RequestParam("date") String date){
        try {
            PageInfo<Services> pageInfo = chaperonService.getServices(date);
            return Result.success(pageInfo);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *预约陪诊服务
     */
    @PostMapping("/services/book")
    public Result createNewServiceBook(@RequestBody BookInfo bookInfo, @RequestHeader("token") String token){
        try {
            BookResponseDTO response = chaperonService.createNewServiceBook(bookInfo,token);
            return Result.success(response);
        } catch (Exception e) {
            return Result.error("预约失败: " + e.getMessage());
        }
    }

    /*
     *取消陪诊服务
     */
    @DeleteMapping("/services/book")
    public Result deleteServiceBook(@RequestParam("orderId") String orderId){
        try {
            chaperonService.deleteServiceBook(orderId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    /*
     *用户获取陪诊列表
     */
    @GetMapping("/services/book")
    public Result getServiceBook(){
        try {
            PageInfo<ServiceBookInfoDTO> pageInfo = chaperonService.getServiceBookList();
            return Result.success(pageInfo);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}